package com.zhuzhe.blog.category.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhuzhe.blog.category.entity.Label;
import com.zhuzhe.blog.redisFinal.CategoryRedisFinal;
import com.zhuzhe.blog.category.requestDto.LableSerachDto;
import com.zhuzhe.blog.base.JsonData;
import com.zhuzhe.blog.base.ResponseData;
import com.zhuzhe.blog.category.mapper.LabelMapper;
import com.zhuzhe.blog.category.responseVo.LabelPageVo;
import com.zhuzhe.blog.category.responseVo.LabelVo;
import com.zhuzhe.blog.category.service.ILabelService;
import com.zhuzhe.blog.convert.Convert;
import com.zhuzhe.blog.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 标签表 服务实现类
 * </p>
 *
 * @author u53
 * @since 2020-09-29
 */
@Service
public class LabelServiceImpl extends ServiceImpl<LabelMapper, Label> implements ILabelService {


    //装配redis
    @Autowired
    private RedisUtil redisUtil;



    @Override
    public ResponseData<IPage<LabelPageVo>> queryPage(LableSerachDto request) {
        IPage<LabelPageVo> label = baseMapper.querLable(request.getPage(),request);
        return JsonData.success(label);
    }

    @Override
    public ResponseData<String> updateLabelById(Label entity) {
        entity.setUpdateDate(new Date());
        super.updateById(entity);
        //修改完毕刷新redis里得分类信息
        refshRedis(entity);
        return JsonData.success(entity.getId());
    }

    @Override
    public ResponseData<String> saveLabel(Label label) {
        super.save(label);
        //修改完毕刷新redis里得分类信息
        refshRedis(label);
        return JsonData.success(label.getId());
    }


    @Override
    public ResponseData<String> removeLabelById(String id) {
        Label label = baseMapper.selectById(id);
        super.removeById(id);
        //修改完毕刷新redis里得分类信息
        refshRedis(label);
        return JsonData.success(id);
    }

    @Override
    public ResponseData<LabelVo> getLabelById(String id) {
        //先从redis取值 如果没有值 查询数据库 讲查询得值存入redis
        Label label = (Label) redisUtil.get(CategoryRedisFinal.LABEL_KEY+id);
        if (null == label){
            label = super.getById(id);
            if (null != label){
                redisUtil.set(CategoryRedisFinal.LABEL_KEY+id,label);
            }
        }
        LabelVo labelVo = new LabelVo();
        Convert.convertType(label,labelVo);
        return JsonData.success(labelVo);
    }

    @Override
    public List<Label> getLabelListByIds(List<String> labelIds) {
        List<Label> labels = labels = baseMapper.selectList(new QueryWrapper<>());
        List<Label> labelsRes = labels.stream().filter(labelentity -> labelIds.contains(labelentity.getId())).collect(Collectors.toList());
        return labelsRes;
    }

    @Override
    public List<String> getListIds(String categoryId) {
        List<Object> labelIds = redisUtil.lGet(CategoryRedisFinal.CATEGOR_LABELS_KEY+categoryId,0,-1);
        List<String> ids = new ArrayList<>();
        if (null == labelIds || labelIds.size() == 0){
            QueryWrapper<Label> wrapper = new QueryWrapper<>();
            wrapper.eq(Label.CATEGORY_ID,categoryId);
            List<Label> labels = super.list(wrapper);
            for (Label label : labels){
                String id = label.getId();
                labelIds.add(id);
            }
            //将id加入list 集合
            convtList(labelIds,ids);
            if (ids !=null && ids.size() != 0){
                redisUtil.lSet(CategoryRedisFinal.CATEGOR_LABELS_KEY+categoryId,ids);
            }
        }else{
            convtList(labelIds,ids);
        }
        return ids;
    }

    private static void convtList(List<Object> labelIds,List<String> ids){
        for (Object id : labelIds){
            String labelID = (String) id;
            ids.add(labelID);
        }
    }


    private  void refshRedis(Label entity){
        //分类及标签
        redisUtil.del(CategoryRedisFinal.FIND_CATEGORY_AND_LABE_LKEY);
        //正常状态分类
        redisUtil.del(CategoryRedisFinal.FIND_ALL_NORMAL_KEY);
        redisUtil.del(CategoryRedisFinal.LABEL_KEY+entity.getId());
        redisUtil.del(CategoryRedisFinal.CATEGOR_LABELS_KEY+entity.getCategoryId());
    }

}
